package de.scs.ui;

import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Ellipse2D;

import javax.swing.JFrame;
import javax.swing.WindowConstants;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.labels.XYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import de.scs.constants.BloodSugarLevelConstants;

public class BloodSugarStatisticsGraph extends JFrame
{

  /**
   * 
   */
  private static final long serialVersionUID = 2781085906635979475L;
  private XYSeries          bsvSeries;
  private JFreeChart        chart;
  private static float      counter          = 0.0f;

  @SuppressWarnings("deprecation")
  public BloodSugarStatisticsGraph(final String title)
  {

    super(title);
    super.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    addWindowListener(new WindowAdapter()
    {
      @Override
      public void windowClosing(WindowEvent e)
      {
        setVisible(false);
      }
    });
    bsvSeries = new XYSeries("Blood Sugar Level");
    XYDataset dataset = createDataset(bsvSeries, BloodSugarLevelConstants.STRIVED_LEVEL, false, false);
    chart = ChartFactory.createXYLineChart(title, "Time (h)", "Blood Sugar Value (mg/dl)", dataset, PlotOrientation.VERTICAL,
      false, true, false);

    XYPlot xyplot = (XYPlot) chart.getPlot();
    xyplot.setBackgroundPaint(Color.lightGray);
    xyplot.setDomainGridlinePaint(Color.white);
    xyplot.setRangeGridlinePaint(Color.white);

    final NumberAxis domainAxis = (NumberAxis) xyplot.getDomainAxis();
    domainAxis.setRange(0,24);
    
    final NumberAxis numberaxis = (NumberAxis) xyplot.getRangeAxis();
    numberaxis.setLowerBound(20);
    numberaxis.setTickUnit(new NumberTickUnit(50), true, true);
    numberaxis.setRange(0, 250);
    
    Ellipse2D.Double circle = new Ellipse2D.Double(-4D, -4D, 8D, 8D);
    XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
    renderer.setSeriesLinesVisible(0, true);
    renderer.setSeriesShapesVisible(0, true);
    renderer.setSeriesPaint(0, Color.BLUE);
    renderer.setSeriesShape(0, circle);
    renderer.setSeriesFillPaint(0, Color.yellow);
    renderer.setSeriesOutlinePaint(0, Color.gray);
    renderer.setUseOutlinePaint(true);
    renderer.setUseFillPaint(true);
    renderer.setToolTipGenerator(new XYToolTipGenerator()
    {

      @Override
      public String generateToolTip(XYDataset arg0, int arg1, int arg2)
      {
        return arg0.getY(arg1, arg2).intValue() + " mg/dl (" + (int) arg0.getXValue(arg1, arg2) + "h)";
      }

    });
    xyplot.setRenderer(0, renderer);

    XYLineAndShapeRenderer renderer2 = new XYLineAndShapeRenderer();
    XYDataset max = createDataset(new XYSeries("Maximum"), 120D, false, true);
    xyplot.setDataset(1, max);
    xyplot.setRenderer(1, renderer2);

    renderer2.setBaseShapesVisible(false);
    renderer2.setSeriesLinesVisible(0, true);
    renderer2.setSeriesShapesVisible(0, false);
    renderer2.setSeriesPaint(0, Color.RED);

    XYLineAndShapeRenderer renderer3 = new XYLineAndShapeRenderer();
    XYDataset min = createDataset(new XYSeries("Minimum"), 90D, false, true);
    xyplot.setDataset(2, min);
    xyplot.setRenderer(2, renderer3);

    renderer3.setBaseShapesVisible(false);
    renderer3.setSeriesLinesVisible(0, true);
    renderer3.setSeriesShapesVisible(0, false);
    renderer3.setSeriesPaint(0, Color.RED);

    final ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new java.awt.Dimension(900, 350));
    setContentPane(chartPanel);
  }

  private static XYDataset createDataset(XYSeries xyseries, double d, boolean genY, boolean genX)
  {
    int[] time = new int[24];
    XYSeriesCollection xyseriescollection = new XYSeriesCollection();
    if (genX)
    {
      for (int i = 0; i < time.length; i++)
      {
        if (genY) d = (Math.random() * 230) + 20;
        xyseries.add(i, d);
      }
    }
    else
      xyseries.add(counter, d);
    xyseriescollection.addSeries(xyseries);
    return xyseriescollection;
  }

  public void setBloodSugarValueInGraph(double value)
  {
    counter += 0.2f;
    this.bsvSeries.add(counter, value);
  }
}
